package org.me.marble;

import android.hardware.*;

import android.app.Activity;
import android.content.Context;

/**
*
* @author tungi
*/

public class MySensorEvent implements SensorEventListener{
    private static Sensor orientation;
    private static SensorManager sensorMgr;
    private static MySensorEvent listener;
    private static MySensorEventListener mlistener;

    public MySensorEvent(){
    }

    public static MySensorEvent register(Activity activity,MySensorEventListener ml){
        listener=new MySensorEvent();
        sensorMgr=(SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
        orientation=sensorMgr.getSensorList(Sensor.TYPE_ORIENTATION).get(0);

	boolean isSupported=sensorMgr.registerListener(listener,
		orientation,SensorManager.SENSOR_DELAY_GAME);
		if(!isSupported){
		    sensorMgr.unregisterListener(listener,orientation);
            return null;
		}
        mlistener=ml;
        return listener;
    }

    public void unregister(){
        if(sensorMgr!=null){
	    sensorMgr.unregisterListener(listener,orientation);
	    sensorMgr=null;
        }
    }

    public void onSensorChanged(SensorEvent event){
        mlistener.onSensorChanged(event);
    }

    public void onAccuracyChanged(Sensor sensor,int accuracy){
    } 
}
